/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 三石
 * Date: 2022-09-13
 * Time: 19:05
 */
public class MylinstNode {

    static class LinstNode{
        public int val;
        public LinstNode pre;
        public LinstNode next;
        public LinstNode(int val){
            this.val=val;
        }
    }
    public LinstNode head;
    public LinstNode last;

    /**
     * 打印双链表
     */
    public void display(){
        LinstNode cur=head;
        while (cur != null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
    }

    /**
     * 寻找关键字key
     * @param key
     * @return
     */
    public boolean find(int key){
        LinstNode cur=head;
        while (cur != null){
            if (cur.val == key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }

    /**
     * 求长度
     */
    public int length(){
        LinstNode cur=head;
        int count=0;
        while (cur != null){
            count++;
            cur=cur.next;
        }
        return count;
    }

    /**
     * 头插
     */
    public void addfirst(int date){
        LinstNode node=new LinstNode( date );
        if (head == null){
            head = node;
            last = node;
        }else {
            node.next = head;
            head.pre = node;
            head = node;
        }
    }

    /**
     * 尾插
     */
    public void addlast(int date){
        LinstNode node = new LinstNode(date);
        if (head == null){
            head = node;
            last = node;
        }else {
           last.next = node;
            node.pre = last;
            last = node;
        }
    }

    /**
     * k下标插入
     */
    public void addanywhere(int k, int date){
      LinstNode node = new LinstNode(date);
      if (k<0 || k>length()){
          System.out.println("下标不合法");
          return;
      }
      if (k == 0){
          addfirst(date);
          return;
      }
      if (k == length()){
          addlast(date);
          return;
      }
        LinstNode cur = head;
        int count = 0;
        while (cur != null){
            if (count == k){
                node.pre = cur.pre;
                cur.pre.next = node;
                node.next = cur;
                cur.pre = node;
                return;
            }
            cur = cur.next;
            count++;
        }
    }


    /**
     * 删除第一个date节点
     */
    public void dell(int date){
        LinstNode node = new LinstNode(date);
        LinstNode cur = head;
        if (cur == null) return;
        while (cur != null){
            if (head.val == date){
                head.next.pre = null;
                head = head.next;
                return;
            }else {
                if (cur.val == date){
                    cur.pre.next = cur.next;
                    cur.next.pre = cur.pre;
                    return;
                }
            }
            cur = cur.next;
        }
    }

    /**
     * 删除所有date节点
     */
    public void dellall(int date){
        LinstNode node = new LinstNode(date);
        LinstNode cur = head;
        if (cur == null) return;
        while (cur != null){
            if (head.val == date){
                head.next.pre = null;
                head = head.next;
            }else {
                if (cur.val == date){
                    cur.pre.next = cur.next;
                    cur.next.pre = cur.pre;
                }
            }
            cur = cur.next;
        }
    }

    /**
     *清空链表
     */
    public LinstNode empty(){
        if (head == null)
            return null;
        LinstNode cur = head.next;
        while (cur != null){
            LinstNode curNext = cur;
            cur.next = null;
            cur.pre = null;
            cur = curNext;
            curNext = curNext.next;
        }
        head=null;
        last=null;
        return head;
    }

}
